<?php

class Users_mod extends CI_Model {

	
	function __construct() 
	{
		// Call the Model constructor
        parent::__construct();
	}
	
	/*
	 * 	funkcija za dodavanje novih korisnika u bazu
	 */	
	public function add_user($data)
	{	
		// provera da li postoji vec korisnik sa istim username-om
		if($this->check_username($data['username'])) //postoji podatak. Nema upisa
			return false;
		else
		{	//ne postoji isti username
            if( true == $this->config->item('admin_password_cript') )
                $data['password'] = md5($data['password']);
            
			$this->db->insert('users',$data); //izvrsi upis
			//return $data;
			return true;
		}
	}
	
	public function check_username($username)
	{
		$sql_check = "SELECT id FROM `users` WHERE `username` = '{$username}' ;";
		$query = $this->db->query($sql_check);
		$users = $query->num_rows(); 
		if($users) //postoji podatak
			return true;
		else
			return false;
	}
	
	public function get_all_users()
	{
		$sql = "SELECT * FROM `users`;";
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	public function get_nr_users()
	{
		$sql = "SELECT * FROM `users`;";
		$query = $this->db->query($sql);
		return $query->num_rows(); 
	}
	
	public function get_users($page=1,$per_page=25)
	{
		$start = ($page-1) * $per_page;
		$sql = "SELECT * FROM `users` LIMIT $start, $per_page ;";
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	public function get_user($id)
	{
		$sql = "SELECT * FROM `users` WHERE `id`= {$id} ;";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	public function edit_user($id,$data)
	{
		//$sql = "UPDATE `users` SET `firstname` = '{$data['firstname']}', `lastname` = '{$data['lastname']}', `e_mail` = '{$data['e_mail']}' WHERE `ID` = {$id};";
		//$query = $this->db->query($sql);
         if( true == $this->config->item('admin_password_cript') )
                $data['password'] = md5($data['password']);
         
		$this->db->where('id', $id);
		$this->db->update('users', $data); 
		return;
	}
	
	public function delete_user($id)
	{
		//$sql = "DELETE FROM `users` WHERE `ID` = {$id}; ";
		//$query = $this->db->query($sql);
		$this->db->where('id', $id);
		$this->db->delete('users'); 
		return;
	}
	
	public function new_password_user($id, $new_password)
	{
		 if( true == $this->config->item(admin_password_cript) )
                $new_password = md5($new_password);
		$sql = "UPDATE `users` SET `password` = '{$new_password}' WHERE `id` = {$id};";
		$query = $this->db->query($sql);
		return $query;
	}
	
	public function find_users($data)
	{		
		$sql_where = "";
		if($data['firstname']!="") $sql_where .= " `firstname` LIKE '%{$data['firstname']}%' ";
		if($data['lastname']!="") 
		{
			if($sql_where!="") $sql_where .= " OR ";
			$sql_where .= " `lastname` LIKE '%{$data['lastname']}%' ";
		}
		if($data['e_mail']!="") 
		{
			if($sql_where!="") $sql_where .= " OR ";
			$sql_where .= " `e_mail` LIKE '%{$data['e_mail']}%' ";
		}
		if($data['username']!="") 
		{
			if($sql_where!="") $sql_where .= " OR ";
			$sql_where .= " `username` LIKE '%{$data['username']}%' ";
		}
		
		if($sql_where != "") $sql_where = " WHERE " . $sql_where;
		
		$sql = "SELECT * FROM `users` " . $sql_where . " LIMIT 0, 25;";
		
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
}

?>